﻿using Microsoft.Win32;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WWNotice.constant;
using WWNotice.service;
using WWNotice.Utils;

namespace WWNotice
{
    public partial class Form1 : Form
    {
        System.Timers.Timer timer = new System.Timers.Timer(1000 * 5);
        System.Timers.Timer timer2 = new System.Timers.Timer(1000 * 7200);
        System.Timers.Timer timer3 = new System.Timers.Timer(1000 * 120);
        private DataHandle dataHandle;
        bool isLogin = false;
        bool isStart = true;
        public Form1()
        {
            InitializeComponent();
            dataHandle = new DataHandle(webBrowser1, textBox1);
            timer.Elapsed += delegate
            {
                Notice.getWindows();
                if (DateTime.Now.Hour == 23 && DateTime.Now.Second < 7 && (DateTime.Now.Minute == 0 || DateTime.Now.Minute == 30))
                {
                    isLogin = false;
                    dataHandle.init();
                }

                if (DateTime.Now.Hour < 23 && DateTime.Now.Hour > 6)
                {
                    try
                    {
                        string a = HttpUtil.GetResponse("http://127.0.0.1:8888/wechatnews/wangwang");
                        string test = HttpUtil.GetResponse("http://47.101.222.82:8888/wechatnews/wangwang");
                        if (!a.StartsWith("{\"code\":200") && a.Length>0)
                        {
                            Notice.errorMessage("旺旺检测服务器异常:" + a);

                        }

                    }
                    catch (Exception e)
                    {

                    }


                }
            };
            timer.Start();
            timer2.Elapsed += delegate
            {
                if (DateTime.Now.Hour >= 9 && isStart)
                {
                    isLogin = false;
                    dataHandle.init();
                }

            };
            timer3.Elapsed += delegate
            {
                if (DateTime.Now.Hour >= 9 && isLogin)
                {
                    isStart = false;
                    HttpServiceApi.serviceEnum = ServiceEnum.noDeliver;
                    webBrowser1.Navigate(string.Format(HttpServiceApi.TAOBAO_NO_DELIVER, DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), null, null, "referer:https://fahuo.cainiao.com/consigns/tb/order.htm");
                }
            };
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            #region
            //以下注释有用，误删
            //Windows Registry Editor Version 5.00

            //[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application / json]

            //"CLSID" = "{25336920-03F9-11cf-8FD0-00AA00686F13}"

            //"Encoding" = dword:00080000

            //  [HKEY_CLASSES_ROOT\MIME\Database\Content Type\text / json]

            //"CLSID" = "{25336920-03F9-11cf-8FD0-00AA00686F13}"

            //"encoding" = dword:00080000
            //RegistryKey rgk = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\application/json", true);
            //rgk.SetValue("Encoding", 524288);
            //rgk = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\text/json", true);
            //rgk.SetValue("Encoding", 524288);
            //rgk.Flush(); //刷新注册表  
            //rgk.Close();
            #endregion
            webBrowser1.Navigate("https://myseller.taobao.com/home.htm");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            isLogin = false;
            dataHandle.init();
            timer2.Start();
            timer3.Start();

        }

        private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            DataHandle dataHandle = new DataHandle(webBrowser1, textBox1);
            RealTimeHandle realTimeHandle = new RealTimeHandle(webBrowser1, textBox1);
            switch (HttpServiceApi.serviceEnum.ToString())
            {
                case "realTimeVisitData":
                    dataHandle.handle1();
                    break;
                case "userAssess":
                    dataHandle.handle2();
                    break;
                case "phoneHomeClick":
                    dataHandle.handle3();

                    break;
                case "deliver":
                    dataHandle.handle4();
                    isLogin = true;
                    break;
                case "noDeliver":
                    realTimeHandle.handle1();
                    isStart = true;
                    break;
            }
        }



        private void Button2_Click(object sender, EventArgs e)
        {
            HttpServiceApi.serviceEnum = ServiceEnum.noDeliver;
            // 订单数据爬取录入
            webBrowser1.Navigate(string.Format(HttpServiceApi.TAOBAO_DELIVER, DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), null, null, "referer:https://fahuo.cainiao.com/consigns/tb/order.htm");
        }
    }


}

